//
//  VideoController.swift
//  amicool
//
//  Created by mario-yc on 2017/6/14.
//  Copyright © 2017年 mario-yc. All rights reserved.
//

import UIKit

private let reuseIdentifier = "VideoCell"

class VideoController: UICollectionViewController {
    var videoList:NSArray!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //修改导航条的背景及文字
        self.navigationController?.navigationBar.barTintColor = UIColor(red:
            20/255.0, green: 155/255.0, blue: 1.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName:UIColor.white]
        
        
        
        // Do any additional setup after loading the view.
        getVideoList()
    }
    func getVideoList(){
        let parameters:NSDictionary = ["SessionID":AMNetworkMngTool.loginReturnModel!.sessionid!,
                                       "mod":"video","page":"1"];AMNetworkMngTool.shared.AMNetwork_GetVideoList(parameters){
                                        (videoList) in
                                        if videoList != nil {
                                            DispatchQueue.main.async{
                                                self.videoList = videoList
                                                self.collectionView?.reloadData()
                                            }
                                        }
                                        else{
                                            //提示，获取视频列表失败
                                        }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using [segue destinationViewController].
     // Pass the selected object to the new view controller.
     }
     */
    
    // MARK: UICollectionViewDataSource
    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        if self.videoList != nil {
            return self.videoList.count
        }
        return 1
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! VideoCell
        
        // Configure the cell
        if self.videoList != nil{
            let model = self.videoList[indexPath.row] as! VideoReturnJson
            cell.title.text = model.name
            cell.author.text = model.author
            
            if model.thumb != nil{
                let imgUrlPre="http://amicool.neusoft.edu.cn/Uploads/"
                let imgUrl = URL(string: imgUrlPre + model.thumb!)
                let imgData = try! Data(contentsOf: imgUrl!)
                cell.img.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80,height:60))
            }
            else{
                cell.img.image = UIImage(named: "placeholder.png")
            }
        }
        
        return cell
    }
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let videoStoryboard = UIStoryboard(name: "Video", bundle: nil)
        let vd = videoStoryboard.instantiateViewController(withIdentifier: "videoDetail") as! VideoDetailController
        //取模型
        vd.videoModle = self.videoList[indexPath.row] as?
        VideoReturnJson
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vd, animated: true)
        self.hidesBottomBarWhenPushed = false
    }
    
    // MARK: UICollectionViewDelegate
    
    /*
     // Uncomment this method to specify if the specified item should be highlighted during tracking
     override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
     return true
     }
     */
    
    /*
     // Uncomment this method to specify if the specified item should be selected
     override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
     return true
     }
     */
    
    
    
}
